import co
import runtime

type atm_t = struct {
    string name
    string address
    f32 amount
    int count
    string bank
}

fn make_atm():fn(f32):atm_t {
    var atm = atm_t{
        name: 'high park',
        address: 'shenzhen',
        amount: 1000,
        count: 0,
        bank: 'china bank',
    }

    return fn(f32 amount):atm_t {
        atm.amount -= amount
        return atm
    }
}

fn main() {
    // 进行 gc 检测 atm 捕获的情况
    runtime.gc()
    co.sleep(500) // wait gc finish

    var atm1_draw = make_atm()
    println(atm1_draw(100).amount)
    println(atm1_draw(100).amount)

    // 进行 gc 检测 atm 捕获的情况
    runtime.gc()
    co.sleep(500) // wait gc finish

    var atm2_draw = make_atm()
    println(atm2_draw(100).amount)
    println(atm2_draw(100).amount)

    // --- list struct
    var list = vec_cap<atm_t>(0)

    list.push(atm_t{
        amount: 100,
    })

    list.push(atm_t{
        amount: 200,
    })
    list.push(atm_t{
        amount: 300,
    })
    list.push(atm_t{
        amount: 400,
    })

    var list2 = vec_cap<atm_t>(100)

    for item in list {
        println(item.amount)
        item.amount = 1000
    }

    // 进行 gc 检测 atm 捕获的情况
    runtime.gc()
    co.sleep(500) // wait gc finish

    for item in list {
        println(item.amount)
    }

    // 进行 gc 检测 atm 捕获的情况
    runtime.gc()
    co.sleep(500) // wait gc finish

    // --- array
    [atm_t;8] array = []

    array[0] = atm_t{
        amount: 100,
    }

    array[3] = atm_t{
        amount: 400,
    }

    array[5] = atm_t{
        amount: 600,
    }

    array[7] = atm_t{
        amount: 800,
    }

    var list3 = vec_cap<atm_t>(200)

    // 进行 gc 检测 atm 捕获的情况
    runtime.gc()
    co.sleep(500) // wait gc finish

    for int i=0; i < 8; i+=1 {
        println(array[i].amount)
    }
}